package com.tencreat.service.pay.impl;

import com.tencreat.common.core.domain.entity.SysDictData;
import com.tencreat.common.enums.BalanceBusinessType;
import com.tencreat.common.enums.BalanceTypeEnum;
import com.tencreat.common.enums.OrderStatus;
import com.tencreat.common.enums.OrderType;
import com.tencreat.common.enums.mall.AllEnum;
import com.tencreat.common.event.PaySuccessEvent;
import com.tencreat.common.utils.DictUtils;
import com.tencreat.common.utils.RabbitSendUtils;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.mall.MallOrder;
import com.tencreat.service.account.IMallUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Stream;

/**
 * 充值支付成功回调
 *
 * @author zixin
 * @company 深圳腾创互联科技有限公司
 * @link https://tencreat.com
 * @date 2023/1/2 14:33
 */

@Service
@Slf4j
@RequiredArgsConstructor
public class RechargePaySuccessListener implements ApplicationListener<PaySuccessEvent> {

    private final RabbitSendUtils rabbitUtils;
    private final IMallUserService mallUserService;

    @Override
    public void onApplicationEvent(PaySuccessEvent paySuccessEvent) {
        MallOrder order = (MallOrder) paySuccessEvent.getSource();
        if (order.getOrderType() != OrderType.RECHARGE) {
            return;
        }

        order.setOrderStatus(OrderStatus.OK);
        rabbitUtils.sendBalanceMessage(order.getUuid(), order.getConsumerRoll(), "1", BalanceBusinessType.RECHARGE, "充值增加消费券", order.getOrderSn(), BalanceTypeEnum.CONSUMER_ROLL);
        rabbitUtils.sendBalanceMessage(order.getUuid(), order.getGoodsRoll(), "1", BalanceBusinessType.GIFT_BY_COMSUMER, "充值增加待结算提货单", order.getOrderSn(), BalanceTypeEnum.WAIT_GOODS_ROLL);
        //减少该用户的代采购额度
        rabbitUtils.sendBalanceMessage(order.getUuid(), order.getConsumerRoll(), "2", BalanceBusinessType.RECHARGE, "充值减少代采购额度", order.getOrderSn(), BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);


        //购买商品的用户
        MallUser mallUser = mallUserService.lambdaQuery().select(MallUser::getId, MallUser::getReferrerUuid).eq(MallUser::getUuid, order.getUuid()).one();
        log.info("购买商品的用户-------{}",mallUser);

        if (Stream.of(OrderType.RECHARGE).anyMatch(orderType -> orderType.compareTo(order.getOrderType()) == 0)) {
            List<SysDictData> wxPayConfig = DictUtils.getDictCache("dividend");
            if (wxPayConfig != null) {
                MallUser parent = mallUserService.lambdaQuery().eq(MallUser::getUuid, mallUser.getReferrerUuid()).one();
                log.info("购买商品的用户的上级用户-------{}",parent);
                if (parent != null) {
                    wxPayConfig.forEach(x -> {
                        BigDecimal amount = order.getConsumerRoll();
                        BigDecimal actualPush = amount.multiply(new BigDecimal(x.getDictValue()).divide(BigDecimal.valueOf(100), 6, BigDecimal.ROUND_DOWN)).setScale(2, BigDecimal.ROUND_DOWN);

                        if ("directReferrer".equals(x.getDictLabel())) {
                            log.info("directReferrer-----{}",actualPush);
                            rabbitUtils.sendBalanceMessage(parent.getUuid(), actualPush, AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.DIRECT_REFERRER, "推荐充值奖励-直推", order.getOrderSn(), BalanceTypeEnum.CONSUMER_ROLL);
                        }
                        if ("indirectReferrer".equals(x.getDictLabel())) {
                            log.info("indirectReferrer-----{}",actualPush);
                            rabbitUtils.sendBalanceMessage(parent.getReferrerUuid(), actualPush, AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.INDIRECT_REFERRER, "推荐充值奖励-间推", order.getOrderSn(), BalanceTypeEnum.CONSUMER_ROLL);
                        }
                    });
                }
            }
        }
    }
}
